package webgoat.utils;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

/**
 * @auth Administrator
 * @date 2019-11-18 12:38
 */
public class JdbcUtil {

    private static Connection link = null;

    static {
        if (link == null) {
            InputStream is = SqlConnection1.class.getClassLoader().getResourceAsStream("druid.properties");
            Properties pro = new Properties();
            try {
                pro.load(is);
                Class.forName(pro.getProperty("driverClassName"));
                link = DriverManager.getConnection(pro.getProperty("url"), pro.getProperty("username"), pro.getProperty("password"));
            } catch (Exception e) {
                e.printStackTrace();

            }

        }
    }

    public static Connection getLink() {
        return link;
    }

    public static void close(Connection conn, Statement st, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (st != null) {
            try {
                st.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 释放资源
     *
     * @param conn
     * @param st
     */
    public static void close(Connection conn, Statement st) {
        close(conn, st, null);
    }


}
